package com.wzu.zlb.ems.controller;

import com.wzu.zlb.ems.pojo.Design;
import com.wzu.zlb.ems.pojo.Order;
import com.wzu.zlb.ems.pojo.Product;
import com.wzu.zlb.ems.pojo.Process;
import com.wzu.zlb.ems.service.OrderService;
import com.wzu.zlb.ems.service.ProcessService;
import com.wzu.zlb.ems.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.*;

@Controller
public class ProductAction {

    @Autowired
    private ProductService productService;
    @Autowired
    private ProcessService processService;
    @Autowired
    private OrderService orderService;

    @GetMapping("/productAdd")
    public String goToProductAdd(HttpSession session,Model model){
        List<Process> processes = processService.findAllProcess();
        model.addAttribute("processes",processes);
        return "productAdd";
    }

    @GetMapping("/product/list")
    public String getProductList(HttpSession session, Model model){
        List<Product> products = productService.findAllProduct();
        model.addAttribute("products",products);
        return "productList";
    }

    @PostMapping("/product/search")
    public String searchProductList(HttpSession session,Model model,Product product){
        if (product.getProductId()==null) product.setProductId(0);
        if (product.getProductName().trim().isEmpty()) product.setProductName(null);
        List<Product> products = productService.searchProduct(product);
        model.addAttribute("products",products);
        return "productList";
    }

    //返回添加结果，0表示添加成功，1表示添加失败，2表示添加异常
    @PostMapping("/product/add")
    @ResponseBody
    public Map<String,String> addProduct(Product product){
        Map<String,String> res = new HashMap<>();
        Integer flag = 0;
        if (product.getProductName().trim().isEmpty()){
            res.put("statue","1");
            res.put("mes","产品名不能为空或空格");
        }else if(product.getCreatedDate().compareTo(new Date())>0){
            res.put("statue","1");
            res.put("mes","开始时间必须不晚于今天！！");
        }else{
            try{
                flag+=productService.addProduct(product);
                if(flag==1)
                    res.put("statue","0");
            }catch (Exception e){
                res.put("statue","2");
            }
        }
        return res;
    }

    //返回删除结果，0表示删除成功，1表示删除失败，2表示删除异常
    @GetMapping("/product/delete/{ids}")
    @ResponseBody
    public Map<String,String> deleteProduct(@PathVariable("ids")String ids){
        Map<String,String> res = new HashMap<>();
        Integer flag = 0;
        try{
            String[] oids = ids.split(",");
            Integer oid;
            for(int i=0;i<oids.length;i++){
                if(oids[i]==null)
                    continue;
                oid= Integer.parseInt(oids[i]);
                flag+=productService.deleteProduct(oid);
            }
            if(flag>=1){
                res.put("statue","0");
            }else{
                res.put("statue","1");
            }
        }catch (Exception e){

        }
        return res;
    }

    //返回修改结果，0表示修改成功，1表示修改失败，2表示修改异常
    @PostMapping("/product/modify")
    @ResponseBody
    public Map<String,String> modifyProduct(Product product){
        Map<String,String> res = new HashMap<>();
        Integer flag = 0;
        List<Order> list=new ArrayList<>();
        try{
            if (product.getProductName().trim().isEmpty()){
                res.put("statue","1");
                res.put("mes","产品名不能为空或空格");
            }else{
                Order order=new Order();
                order.setProductName(productService.findProductById(product.getProductId()).getProductName());
                order.setOrderId(0);
                list=orderService.searchOrders(order);
                flag+=productService.modifyProduct(product);
            }
            if (flag>=1){
                res.put("statue","0");
                list.forEach(a->a.setProductName(product.getProductName()));
                list.forEach(a->orderService.modifyOrderPro(a));
            }
        }catch (Exception e){
            res.put("statue","2");
        }

        return res;
    }

    //查询工艺路线
    @GetMapping("/product/design/{id}")
    public String getDesignList(HttpSession session,Model model,@PathVariable("id") Integer id){
        Product product = productService.findProductById(id);
        model.addAttribute("product",product);
        List<Design> designs = productService.findDesignsByProductId(id);
        model.addAttribute("designs",designs);
        List<Process> processes = processService.findAllProcess();
        model.addAttribute("processes",processes);
        return "productDesign";
    }

    //查询产品详情
    @GetMapping("/product/detail/{id}")
    public String getProductDetail(HttpSession session,Model model,@PathVariable("id")Integer id){
        List<Process> processes = processService.findAllProcess();
        Product product = productService.findProductById(id);
        List<Design> designs = productService.findDesignsByProductId(id);
        model.addAttribute("designs",designs);
        model.addAttribute("product",product);
        model.addAttribute("processes",processes);
        return "productDetail";
    }
    //查询工艺列表



    //新增工序
    @GetMapping("/design/add/{productId}/{processId}")
    @ResponseBody
    public Map<String,String> addDesign(@PathVariable("productId")Integer productId,
                                        @PathVariable("processId")Integer processId){
        Map<String,String> res = new HashMap<>();
        Integer flag = 0;
        Design design = new Design();
        design.setProductId(productId);
        design.setProcessId(processId);
        try{
            flag+=productService.addDesign(design);
            res.put("statue","0");
        }catch (Exception e){
            res.put("statue","1");
        }

        return res;
    }
    //删除工序
    @GetMapping("/design/delete/{productId}/{order}")
    @ResponseBody
    public Map<String,String> deleteDesign(@PathVariable("productId")int productId,
                                           @PathVariable("order")int order){
        Map<String,String> res = new HashMap<>();
        Integer flag = 0;
        try{
            Design design = new Design();
            design.setProductId(productId);
            design.setOrder(order);
            flag+=productService.deleteDesign(design);
            if(flag==1){
                res.put("statue","0");
            }

        }catch (Exception e){
            res.put("statue","1");
        }

        return res;
    }

}
